package com.minminaya.source;

public class Algorithm_12 {

    public static class ListNode {
        int value;
        ListNode next;
    }

    /**
     * @param k 倒数第k个节点
     */
    public static ListNode findKthToTail(ListNode head, int k) {
        if (k < 1 || head == null) {
            return null;
        }

        ListNode pointer = head;

        for (int i = 0; i < k; i++) {
            if (pointer.next != null) {
                pointer = pointer.next;
            } else {
                return null;
            }
        }
        //终点条件为pointer节点到尾部
        while (pointer.next != null) {
            head = head.next;
            pointer = pointer.next;
        }

        return head;
    }


}
